<template>
  <div>
    <router-link to="/search/result">
      <div class="search">
        <div>
          <img src="@/icon/search1.png" alt="" @click="searchs" />
        </div>
        <div>
          <input
            type="text"
            @keyup.enter="searchs"
            placeholder="请输入关键字"
            v-model="inpSearch"
          />
        </div>
        <div>
          <img src="@/icon/tiaoshi.png" alt="" />
        </div>
      </div>
    </router-link>
    <div class="hisSerachhis" v-if="isShow">
      <div
        v-for="item in hisSearchs"
        :key="item.date"
        @click="hischangesearch(item.title)"
      >
        <span>{{ item.title }}</span> <span @click="removeHistroy(item.title)">×</span>
      </div>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      inpSearch: "",
      pageNum: 1,
      hisSearchs:
        JSON.parse(window.localStorage.getItem("hisSearchs")) == null
          ? []
          : JSON.parse(window.localStorage.getItem("hisSearchs")),
      isShow: false,
    };
  },

  watch: {
    $route: {
      handler: function (val) {
        if (val.path == "/search/result") {
          this.isShow = true;
        } else {
          this.isShow = false;
        }
      },
      // 深度观察监听
    },
  },
  methods: {
    removeHistroy(item) {
      this.hisSearchs =  this.hisSearchs.filter((el) => {
        return el.title !=item
      });
      window.localStorage.setItem(
        "hisSearchs",
        JSON.stringify(this.hisSearchs)
      );
    },
    hischangesearch(title) {
      this.inpSearch = title;
      this.searchs();
    },
    searchs() {
      this.isShow= true,
      console.log(JSON.parse(window.localStorage.getItem("hisSearchs")));
      if (this.inpSearch.length < 1) {
        return;
      }
      this.$axios
        .get(
          `https://apis.netstart.cn/bcomic/Search?styleId=-1&areaId=2&isFinish=-1&order=-1&pageNum=${this.pageNum}&pageSize=10&isFree=-1&keyWord=${this.inpSearch}`
        )
        .then((result) => {
          // console.log();
          this.$emit("search-sjuset", result.data.data.list);
        })
        .catch((err) => {
          console.log(err);
        });
      let ss = 0;
      this.hisSearchs.forEach((item) => {
        if (item.title == this.inpSearch) {
          ss = 1;
        }
      });
      if (ss > 0) {
        return;
      }
      this.hisSearchs.push({
        title: this.inpSearch,
        date: new Date().getTime(),
      });

      function sortBy(attr, rev) {
        if (rev == undefined) {
          rev = 1;
        } else {
          rev ? 1 : -1;
        }
        return function (a, b) {
          a = a[attr];
          b = b[attr];
          if (a < b) {
            return rev * -1;
          }
          if (a > b) {
            return rev * 1;
          }
          return 0;
        };
      }
      this.hisSearchs.sort(sortBy("date", -1));
      window.localStorage.setItem(
        "hisSearchs",
        JSON.stringify(this.hisSearchs)
      );
    },
  },
  created() {
    if (this.$route.path == "/search/result") {
      this.isShow = true;
    } else {
      this.isShow = false;
    }
  },
};
</script>

<style lang="less" scoped>
.search {
  width: 375rem;
  padding-top: 60rem;
  display: flex;
  align-items: center;
  color: @color2;
  input {
    width: 295rem;
    height: 35rem;
    border-radius: 10rem;
    background-color: @color4;
    border: none;
    outline: none;
    padding-left: 40rem;
    margin: 0 auto;
    color: @color2;
  }
  img:nth-child(1) {
    position: relative;
    left: 35rem;
    width: 18rem;
    height: 18rem;
  }

  > div:nth-child(3) {
    position: relative;
    right: 70rem;
    img {
      width: 18rem;
      height: 18rem;
    }
  }
}
.hisSerachhis {
  width: 335rem;

  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  margin-top: 15rem;
  > div {
   
    height: 25rem;
    color: @color4;
    border-radius: 10rem;
    text-align: center;
    background-color: @color3;
    margin-right: 10rem;
    line-height: 25rem;
     margin-top: 7rem;
     padding-right: 10rem;
    span:nth-child(1){
     
    margin-right: 12rem;
    padding-left: 15rem;
    padding-right: 15rem;
    }
    span:nth-child(2) {
      
      font-size: 14rem;
    }
  }
}
</style>